/**
	* @description 定义了Globalseo基本的样式组件，icon，button，灯塔，头部
	* @author: xingjun.yexj
	* @time: 2011-6-17
*/

function fruit(){
	var name = '';
	var imgUrl = '';
}

fruit.prototype = {
	init:function( o ){
		
	},
	
	getName:function(){
		return this.name;
	},
	
	getImgUrl:function(){
		return this.imgUrl;
	}
}




function RememberTheFruit(){
	this.fruitList = [
		{
			name:'苹果',
			ename:'Apple',
			imgUrl:'Apple128.png'
		},{
			name:'杏子',
			ename:'Apricot',
			imgUrl:'Apricot128.png'
		},{
			name:'香蕉',
			ename:'Banana',
			imgUrl:'Banana128.png'
		},{
			name:'樱桃',
			ename:'Cherry',
			imgUrl:'Cherry128.png'
		},{
			name:'猕猴桃',
			ename:'Kiwi',
			imgUrl:'Kiwi128.png'
		},{
			name:'柠檬',
			ename:'Lemon',
			imgUrl:'Lemon128.png'
		},{
			name:'芒果',
			ename:'Mango',
			imgUrl:'Mango128.png'
		},{
			name:'橘子',
			ename:'Orange',
			imgUrl:'Orange128.png'
		},{
			name:'桃子',
			ename:'Peach',
			imgUrl:'Peach128.png'
		}
		
	];
	
	this.fruitTotal = this.fruitList.length;	//水果的总数	

	this.currentFruitNum = 3;			//第一关显示四个数字
	this.currentGateNum = 0;			//关数, 0为第一关。

	
	/*
	this.result = [
		{
			gateNum:1,
			fruitNum:3,
			testRandomList: [],
			userClickList:[]
		},
	]
	*/
	this.result = [];



}

RememberTheFruit.prototype = {
	init:function(){
		this.initDom();
		this.initEvent();
		this.welcome();
	},
	
	initDom: function(){
		var elid = '#fruitAll';
		var elTemplateId = '#fruitOneTemplate';
		var fruitList = this.fruitList;
		
		jQuery.each(fruitList, function(index, value){
			var templateEl =  $(elTemplateId).clone();
			templateEl.attr('id',$(elTemplateId).attr('id') + index);
			templateEl.find('.fimg').attr('src', 'res/images/fruit/' + value.ename + '128.png');
			templateEl.find('.textBox').html(value.name);
			templateEl.find('.fruitListIndex').val(index);
			templateEl.appendTo(elid).show();
		})
	},
	
	initEvent:function(){
		var _self = this;
		var userClickList = [];
		$("#fruitAll").delegate('.fruit', 'click', function(){
			
			$('#mainTips').html( $(this).find('.textBox').html() ).css('opacity',1).show().animate({
				opacity: 0
			  }, 500, function() {
			});
			
			var num = parseInt( $(this).find('.fruitListIndex').val() );
			userClickList.push( num );
			
			
			if(userClickList.length >= _self.currentFruitNum){
				_self.result[_self.currentGateNum].userClickList = userClickList;
				userClickList = [];
				$('#fruitAll').fadeOut('show',function(){
					_self.showResultGate();
					_self.judgeGate();
				});
				
				
			}
		});
				
		$('#fleshGate').bind('click',function(){
			_self.fleshGate();
			
		})
		
		$('#nextGate').bind('click',function(){
			_self.nextGate();
			
		})
		

	},
	
	welcome:function(){
		var _self = this;
		$('#welcome, #help').bind('click',function(){
			$('#help').hide();
			$('#welcome').fadeOut('fase',function(){
				_self.start();
				_self.statusGate();
			})
			
		})
	},
	
	start:function(){
		$('#resultGate').hide();
		$('#fruitAll').hide();
		
		this.showFruitOne();
		this.createGate();
	},
	
	judgeGate:function(){
		var _self = this;
		var test = this.result[_self.currentGateNum].testRandomList;
		var user = this.result[_self.currentGateNum].userClickList;
		
		var passFlag = true;
		for(var i = 0; i < _self.currentFruitNum; i++){
			if(test[i] !== user[i]){
				passFlag = false;
			}
		}
		
		if(passFlag){
			$('#resultGate').show();
			$('#resultGate .infoYes').show();
			$('#resultGate .infoNo').hide();
			$('#resultGate #nextGate').show();
		}else{
			$('#resultGate').show();
			$('#resultGate .infoYes').hide();
			$('#resultGate .infoNo').show();
			$('#resultGate #nextGate').hide();
		}
	},
	
	createGate: function(){
		var _self = this;
		var randomList = _self.getRandom( _self.currentFruitNum );
		_self.result[_self.currentGateNum] = {
			gateNum:this.currentGateNum,
			fruitNum:this.currentFruitNum,
			testRandomList: randomList,
			userClickList:[]
		};
		
		var fruitList = _self.fruitList;
		var fruitOne = $('#fruitOne');
		
		$('#fruitOne .fruitStep').val('0');
		
		var delayTime = 2000;
		
		if(_self.currentFruitNum == 9){
			delayTime = 500;
		}
		
		var handle = _self.later(delayTime, _self, function(){
			var fruitStep = parseInt( $('#fruitOne .fruitStep').val() );
			
			if(fruitStep >= _self.currentFruitNum){
				handle.cancel();
				
				$('#fruitOne').fadeOut('fast',function(){
					fruitOne.find('.fimg').attr('src', 'res/images/prepare.png');
					fruitOne.find('.textBox').html('');
					_self.showFruitAll();
				});
				
				return false;
			}
			
			var value = fruitList[ randomList[fruitStep] ];
			
			fruitOne.fadeOut('fast', function() {
				fruitOne.find('.fimg').attr('src', 'res/images/fruit/' + value.imgUrl);
				fruitOne.find('.textBox').html(value.name);
				fruitOne.fadeIn('fast', function() {
					
				})
			});
			
			$('#fruitOne .fruitStep').val(fruitStep + 1);
		},[], true);
		
		
	},
	
	showFruitOne:function(){
		var elid = '#fruitOne';
		$(elid).fadeIn('show',function(){});
	},
	
	showFruitAll: function(){
		var elid = '#fruitAll';
		$(elid).fadeIn('show',function(){});
	},
	
	showResultGate: function(){
		var elid = '#resultGate';
		$(elid).fadeOut('show',function(){});
	},
	
	nextGate: function(){
		this.currentFruitNum = this.currentFruitNum + 1;
		this.currentGateNum = this.currentGateNum + 1;
		
		if(this.currentFruitNum == 10){
			$('#resultGate').hide();
			$('#welcome').show();
			$('#noway').show();
			return false;
		}
		
		this.statusGate();
		this.start();
	},
	
	fleshGate:function(){
		if(this.currentFruitNum == 9){
			this.currentFruitNum = 3;
			this.currentGateNum = 0;
		}
		
		this.statusGate();
		this.start();
	},
	
	statusGate:function(){
		$('#statusGate').show();
		$('#currentGateNum').html( this.currentGateNum + 1 );
		$('#currentFruitNum').html( this.currentFruitNum );
	},
	
	/*== private function ==*/
	getRandom: function( n ){
		if(n == 'undefined'){
			n = 1
		}
		
		var randomList = [];
		
		for(var i = 0; i< n; i++){
			var num = Math.random();
			num = parseInt(num * 10);
			if(num == 9){num = num - 1}
			randomList.push(num);
		}
		
		return randomList;
		
	},
	
	later: function(when, o, fn, data, periodic){
		when = when || 0;
		data = data;

		var cancelled = false,
			method = fn,
			wrapper = function() {
				if (!cancelled) {
					if (!method.apply) {
						method(data[0], data[1], data[2], data[3]);
					} else {
						method.apply(o, data || NO_ARGS);
					}
				}
			},
			id = (periodic) ? setInterval(wrapper, when) : setTimeout(wrapper, when);

		return {
			id: id,
			interval: periodic,
			cancel: function() {
				cancelled = true;
				if (this.interval) {
					clearInterval(id);
				} else {
					clearTimeout(id);
				}
			}
		};
	}
	
}